<template>
  <div class='container'>
    <el-dialog :title="formData.id?'编辑文章':'添加文章'" :visible="articlesDialog" width="600px" @close="closeDialog">
      <el-form ref="form" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="文章标题：" prop="title">
          <el-input v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容：" prop="articleBody">
          <quill-editor ref="texts" @blur="bodyRule" v-model="formData.articleBody" :options="editorOption"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址：">
          <el-input v-model="formData.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="publish">确 定</el-button>
      </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/articles.js'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
// 工具栏
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  ['blockquote', 'code-block'], // 引用  代码块-----['blockquote', 'code-block']
  ['image', 'link'] // 链接、图片、视频-----['link', 'image', 'video']
]
export default {
  name: 'articlesAdd',
  props: {
    articlesDialog: {
      type: Boolean,
      default: false
    }
  },
  components: {
    quillEditor
  },
  data () {
    return {
      // 文本编辑器配置
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions
          }
        }
      },
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题～～～', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章正文～～～', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDialog () {
      this.$emit('colseDialog')
      this.formData = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.form.resetFields()
    },
    // 富文本编辑表单验证
    bodyRule () {
      this.$refs.form.validateField('articleBody')
    },
    publish () {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        let res = {}
        if (this.formData.id) {
          res = await update(this.formData)
        } else {
          res = await add(this.formData)
        }
        if (res.status === 200) {
          this.$message.success(`${this.formData.id ? '编辑' : '添加'}成功`)
          this.closeDialog()
        }
      })
    },
    async getArticles (id) {
      const { data: res } = await detail(id)
      this.formData = res
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .ql-editor {
  height: 200px;
}
</style>
